package com.example.cpms.controller;
import com.example.cpms.entity.Building;
import com.example.cpms.service.BuildingService;
import com.example.cpms.utils.PageBean;
import com.example.cpms.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@Slf4j
@RestController
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

//    /**
//     * 查询全部建筑信息
//     * @return
//     */
//    @GetMapping("/park/building/show")  //作废 改用分页查询
//    public Result list(){
//        log.info("查询全部楼宇信息");
//
//       List<Building> buildingList =  buildingService.list();
//
//       return Result.success(buildingList);
//    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/park/building")  //改用PageHelper插件
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,//前端传入要查询的页数以及每页显示多少条数据
                       String name){ //模糊查询额外传入的值，可选
        log.info("楼宇分页查询：{}，{},{}",page,pageSize,name); //输出日志信息
        PageBean pageBean = buildingService.page(page,pageSize,name); //把前端传入的信息传到service层
        return Result.success(pageBean);//返回数据
    }


    /**
     *根据id删除楼宇
     * @return
     */
    @DeleteMapping("/park/building/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除楼宇：{}",id);
        buildingService.delete(id);
        return Result.success();
    }

    /**
     * 新增楼宇
     * @param building
     * @return
     */
    @PostMapping("/park/building")
    public Result insert(@RequestBody Building building){
        log.info("新增楼宇：{}",building);//打印日志信息
        buildingService.add(building);
            return Result.success();
    }

    /**
     * 编辑楼宇信息
     * @param building
     * @return
     */
    @PutMapping("/park/building")
    public Result updata(@RequestBody Building building){
        log.info("编辑楼宇：{}",building);
        buildingService.update(building);
        return Result.success();
    }



}
